#include "ManageTableDB.h"
#include "LogComm.h"
#include "globe.h"
#include "servant/Application.h"
#include "SocialServant.h"
#include "ServiceDefine.h"
#include "SocialServer.h"
#include "TimeUtil.h"

using namespace dataproxy;

// 获取俱乐部进行中的桌子数量
tars::Int32 ManageTableGetCount(const DBAgentServantPrx prx, tars::Int64 cId, int &count)
{
    FUNC_ENTRY("");
    int iRet = 0;
    __TRY__

    if (!prx)
    {
        ROLLLOG_ERROR << "ManageTableGetCount prx null!" << endl;
        return -1;
    }

    dataproxy::TReadDataReq dataReq;
    dataReq.resetDefautlt();
    dataReq.keyName = I2S(E_REDIS_TYPE_HASH) + ":" + I2S(CLUB_TABLE_COUNT) + ":" + L2S(cId);
    dataReq.operateType = E_REDIS_COUNT;
    dataReq.clusterInfo.resetDefautlt();
    dataReq.clusterInfo.busiType = E_REDIS_PROPERTY;
    dataReq.clusterInfo.frageFactorType = E_FRAGE_FACTOR_USER_ID;
    dataReq.clusterInfo.frageFactor = cId;

    vector<TField> fields;
    dbagent::TField tfield;
    auto &conditionFields = dataReq.paraExt.fields;
    tfield.resetDefautlt();
    tfield.colValue = L2S(3);
    conditionFields["status"] = tfield;

    TReadDataRsp dataRsp;
    iRet = prx->redisRead(dataReq, dataRsp);
    if(iRet != 0 || dataRsp.iResult != 0)
    {
        ROLLLOG_ERROR << "ManageTableGetCount failed!" << " iRet:" << iRet << ", iResult:" << dataRsp.iResult << ", dataReq:" << printTars(dataReq) << endl;
        return -2;
    }

    ROLLLOG_DEBUG << "ManageTableGetCount ok!" << " dataReq:" << printTars(dataReq) << ", dataRsp:" << printTars(dataRsp) << endl;
    if (dataRsp.totalcount > 0)
    {
        count = dataRsp.totalcount;
    }

    __CATCH__
    FUNC_EXIT("", iRet);
    return iRet;
}

// 获取牌桌信息
tars::Int32 ManageTableList(const DBAgentServantPrx prx, int flag, long uId, int gameType, vector<long> cIdList, vector<Club::ClubTableInfo> &infos)
{
    FUNC_ENTRY("");
    int iRet = 0;
    __TRY__

    if (!prx)
    {
        ROLLLOG_ERROR << "ManageTableList prx null!" << endl;
        return -1;
    }

    int count = 0;
    std::string cIdKey;
    for (auto cid : cIdList)
    {
        if (count == 0)
        {
            cIdKey += "(";
        }
        else
        {
            cIdKey += ",";
        }
        cIdKey += L2S(cid);
        count++;
    }
    cIdKey += ")";


    dbagent::TDBReadReq dataReq;
    dataReq.keyIndex = 0;
    dataReq.queryType = dbagent::E_SELECT;
    dataReq.tableName = "tb_club_table";

    vector<dbagent::TField> fields;
    dbagent::TField tfield;
    tfield.colArithType = E_NONE;
    tfield.colName = "club_id";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "room_key";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "room_name";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "create_name";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "create_avatar";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "dalao";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "room_type";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "game_type";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "status";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "small_blind";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "big_blind";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "pre_blind";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "max_time";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "cost_time";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "start_time";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "cur_seat";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "max_seat";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "insure";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "entry_fee";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "service_fee";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "score";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "delay_signup";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "rebuy_count";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "bet_limit";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "media";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "blind";
    fields.push_back(tfield);
    
    dataReq.fields = fields;

    //where条件组
    vector<dbagent::ConditionGroup> conditionGroups;
    dbagent::ConditionGroup conditionGroup;
    conditionGroup.relation = dbagent::AND;
    vector<dbagent::Condition> conditions;
    dbagent::Condition condition;
    
    condition.condtion = dbagent::E_IN;
    condition.colType = dbagent::BIGINT;
    condition.colName = "club_id";
    condition.colValues = cIdKey;
    conditions.push_back(condition);

    condition.condtion = dbagent::E_LT;
    condition.colType = dbagent::INT;
    condition.colName = "status";
    condition.colValues = I2S(3);
    conditions.push_back(condition);

    // 只查某一个类型
    if (gameType != 0)
    {
        condition.condtion = dbagent::E_EQ;
        condition.colType = dbagent::INT;
        condition.colName = "game_type";
        condition.colValues = I2S(gameType);
        conditions.push_back(condition);
    }

    conditionGroup.condition = conditions;
    conditionGroups.push_back(conditionGroup);
    dataReq.conditions = conditionGroups;

    // 私人场
    if (flag == 0 && uId > 0)
    {
        conditions.clear();

        condition.condtion = dbagent::E_EQ;
        condition.colType = dbagent::BIGINT;
        condition.colName = "created_id";
        condition.colValues = L2S(uId);
        conditions.push_back(condition);

        condition.condtion = dbagent::E_LT;
        condition.colType = dbagent::INT;
        condition.colName = "status";
        condition.colValues = I2S(3);
        conditions.push_back(condition);

        conditionGroup.condition = conditions;
        conditionGroups.push_back(conditionGroup);
        dataReq.conditions = conditionGroups;
        dataReq.groupRelation = dbagent::OR;
    }

    // 指定返回的行数的最大值
    dataReq.limit = 100;
    // 指定返回的每一行的偏移量
    dataReq.limit_from = 0;

    dbagent::TDBReadRsp dataRsp;
    iRet = prx->read(dataReq, dataRsp);
    if (iRet != 0 || dataRsp.iResult != 0)
    {
        ROLLLOG_ERROR << "ManageTableList failed!" << " iRet:" << iRet << ", iResult:" << dataRsp.iResult << ", dataReq:" << printTars(dataReq) << endl;
        return -1;
    }

    Club::ClubTableInfo info;
    for (auto it = dataRsp.records.begin(); it != dataRsp.records.end(); ++it)
    {
        for (auto itField = it->begin(); itField != it->end(); ++itField)
        {
            if (itField->colName == "club_id")
            {
                info.cId = S2L(itField->colValue);
            }
            else if (itField->colName == "room_key")
            {
                info.roomKey = itField->colValue;
            }
            else if (itField->colName == "room_name")
            {
                info.roomName = itField->colValue;
            }
            else if (itField->colName == "create_name")
            {
                info.createName = itField->colValue;
            }
            else if (itField->colName == "create_avatar")
            {
                info.createAvatar = itField->colValue;
            }
            else if (itField->colName == "dalao")
            {
                info.dalao = S2I(itField->colValue);
            }
            else if (itField->colName == "room_type")
            {
                info.roomType = S2I(itField->colValue);
            }
            else if (itField->colName == "game_type")
            {
                info.gameType = S2I(itField->colValue);
            }
            else if (itField->colName == "status")
            {
                info.status = S2I(itField->colValue);
            }
            else if (itField->colName == "small_blind")
            {
                info.smallBlind = S2I(itField->colValue);
            }
            else if (itField->colName == "big_blind")
            {
                info.bigBlind = S2I(itField->colValue);
            }
            else if (itField->colName == "pre_blind")
            {
                info.preBlind = S2I(itField->colValue);
            }
            else if (itField->colName == "max_time")
            {
                info.maxTime = S2I(itField->colValue);
            }
            else if (itField->colName == "cost_time")
            {
                info.costTime = S2I(itField->colValue);
            }
            else if (itField->colName == "start_time")
            {
                info.startTime = S2I(itField->colValue);
            }
            else if (itField->colName == "cur_seat")
            {
                info.curSeat = S2I(itField->colValue);
            }
            else if (itField->colName == "max_seat")
            {
                info.maxSeat = S2I(itField->colValue);
            }
            else if (itField->colName == "insure")
            {
                int insure = S2I(itField->colValue);
                info.insure = insure == 1 ? true : false;
            }
            else if (itField->colName == "entry_fee")
            {
                info.iEntryFee = S2I(itField->colValue);
            }
            else if (itField->colName == "service_fee")
            {
                info.iServiceFee = S2I(itField->colValue);
            }
            else if (itField->colName == "score")
            {
                info.iScore = S2I(itField->colValue);
            }
            else if (itField->colName == "rebuy_count")
            {
                info.iReBuyCount = S2I(itField->colValue);
            }
            else if (itField->colName == "delay_signup")
            {
                info.iDelaySignUp = S2I(itField->colValue);
            }
            else if (itField->colName == "bet_limit")
            {
                info.bBetLimit = S2I(itField->colValue);
            }
            else if (itField->colName == "media")
            {
                info.bMedia = S2I(itField->colValue);
            }
            else if (itField->colName == "blind")
            {
                info.bBlind = S2I(itField->colValue);
            }
        }

        infos.push_back(info);
    }

    return iRet;

    __CATCH__
    
    FUNC_EXIT("", iRet);
    return -1;
}

tars::Int32 ManageTableListTwo(const DBAgentServantPrx prx, int gameType, vector<long> cIdList, vector<Club::ClubTableInfo> &infos)
{
    FUNC_ENTRY("");
    int iRet = 0;
    __TRY__

    if (!prx)
    {
        ROLLLOG_ERROR << "ManageTableListTwo prx null!" << endl;
        return -1;
    }

    int count = 0;
    std::string cIdKey;
    for (auto cid : cIdList)
    {
        if (count == 0)
        {
            cIdKey += "(";
        }
        else
        {
            cIdKey += ",";
        }
        cIdKey += L2S(cid);
        count++;
    }
    cIdKey += ")";


    dbagent::TDBReadReq dataReq;
    dataReq.keyIndex = 0;
    dataReq.queryType = dbagent::E_SELECT;
    dataReq.tableName = "tb_club_table";

    vector<dbagent::TField> fields;
    dbagent::TField tfield;
    tfield.colArithType = E_NONE;
    tfield.colName = "club_id";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "room_key";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "room_name";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "create_name";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "create_avatar";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "dalao";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "room_type";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "game_type";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "status";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "small_blind";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "big_blind";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "pre_blind";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "max_time";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "cost_time";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "start_time";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "cur_seat";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "max_seat";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "insure";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "entry_fee";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "service_fee";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "score";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "delay_signup";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "rebuy_count";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "bet_limit";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "media";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "blind";
    fields.push_back(tfield);

    dataReq.fields = fields;

    //where条件组
    vector<dbagent::ConditionGroup> conditionGroups;
    dbagent::ConditionGroup conditionGroup;
    conditionGroup.relation = dbagent::AND;
    vector<dbagent::Condition> conditions;
    dbagent::Condition condition;
    
    condition.condtion = dbagent::E_IN;
    condition.colType = dbagent::BIGINT;
    condition.colName = "club_id";
    condition.colValues = cIdKey;
    conditions.push_back(condition);

    condition.condtion = dbagent::E_LT;
    condition.colType = dbagent::INT;
    condition.colName = "status";
    condition.colValues = I2S(3);
    conditions.push_back(condition);

    condition.condtion = dbagent::E_EQ;
    condition.colType = dbagent::INT;
    condition.colName = "dalao";
    condition.colValues = I2S(1);
    conditions.push_back(condition);

    condition.condtion = dbagent::E_EQ;
    condition.colType = dbagent::INT;
    condition.colName = "sync_union";
    condition.colValues = I2S(1);
    conditions.push_back(condition);

    // 只查某一个类型
    if (gameType != 0)
    {
        condition.condtion = dbagent::E_EQ;
        condition.colType = dbagent::INT;
        condition.colName = "game_type";
        condition.colValues = I2S(gameType);
        conditions.push_back(condition);
    }

    conditionGroup.condition = conditions;
    conditionGroups.push_back(conditionGroup);
    dataReq.conditions = conditionGroups;

    // 指定返回的行数的最大值
    dataReq.limit = 100;
    // 指定返回的每一行的偏移量
    dataReq.limit_from = 0;

    dbagent::TDBReadRsp dataRsp;
    iRet = prx->read(dataReq, dataRsp);
    if (iRet != 0 || dataRsp.iResult != 0)
    {
        ROLLLOG_ERROR << "ManageTableListTwo failed!" << " iRet:" << iRet << ", iResult:" << dataRsp.iResult << ", dataReq:" << printTars(dataReq) << endl;
        return -2;
    }

    Club::ClubTableInfo info;
    for (auto it = dataRsp.records.begin(); it != dataRsp.records.end(); ++it)
    {
        for (auto itField = it->begin(); itField != it->end(); ++itField)
        {
            if (itField->colName == "club_id")
            {
                info.cId = S2L(itField->colValue);
            }
            else if (itField->colName == "room_key")
            {
                info.roomKey = itField->colValue;
            }
            else if (itField->colName == "room_name")
            {
                info.roomName = itField->colValue;
            }
            else if (itField->colName == "create_name")
            {
                info.createName = itField->colValue;
            }
            else if (itField->colName == "create_avatar")
            {
                info.createAvatar = itField->colValue;
            }
            else if (itField->colName == "dalao")
            {
                info.dalao = S2I(itField->colValue);
            }
            else if (itField->colName == "room_type")
            {
                info.roomType = S2I(itField->colValue);
            }
            else if (itField->colName == "game_type")
            {
                info.gameType = S2I(itField->colValue);
            }
            else if (itField->colName == "status")
            {
                info.status = S2I(itField->colValue);
            }
            else if (itField->colName == "small_blind")
            {
                info.smallBlind = S2I(itField->colValue);
            }
            else if (itField->colName == "big_blind")
            {
                info.bigBlind = S2I(itField->colValue);
            }
            else if (itField->colName == "pre_blind")
            {
                info.preBlind = S2I(itField->colValue);
            }
            else if (itField->colName == "max_time")
            {
                info.maxTime = S2I(itField->colValue);
            }
            else if (itField->colName == "cost_time")
            {
                info.costTime = S2I(itField->colValue);
            }
            else if (itField->colName == "start_time")
            {
                info.startTime = S2I(itField->colValue);
            }
            else if (itField->colName == "cur_seat")
            {
                info.curSeat = S2I(itField->colValue);
            }
            else if (itField->colName == "max_seat")
            {
                info.maxSeat = S2I(itField->colValue);
            }
            else if (itField->colName == "insure")
            {
                int insure = S2I(itField->colValue);
                info.insure = insure == 1 ? true : false;
            }
            else if (itField->colName == "entry_fee")
            {
                info.iEntryFee = S2I(itField->colValue);
            }
            else if (itField->colName == "service_fee")
            {
                info.iServiceFee = S2I(itField->colValue);
            }
            else if (itField->colName == "score")
            {
                info.iScore = S2I(itField->colValue);
            }
            else if (itField->colName == "rebuy_count")
            {
                info.iReBuyCount = S2I(itField->colValue);
            }
            else if (itField->colName == "delay_signup")
            {
                info.iDelaySignUp = S2I(itField->colValue);
            }
            else if (itField->colName == "bet_limit")
            {
                info.bBetLimit = S2I(itField->colValue);
            }
            else if (itField->colName == "media")
            {
                info.bMedia = S2I(itField->colValue);
            }
            else if (itField->colName == "blind")
            {
                info.bBlind = S2I(itField->colValue);
            }
        }

        infos.push_back(info);
    }

    return iRet;

    __CATCH__
    
    FUNC_EXIT("", iRet);
    return -1;
}

// 获取俱乐部桌子
tars::Int32 ManageTableGetData(const DBAgentServantPrx prx, long uId, string roomKey, int createdTime, Club::ClubTableInfo &info)
{
    FUNC_ENTRY("");
    int iRet = 0;
    __TRY__

    if (!prx)
    {
        ROLLLOG_ERROR << "ManageTableGetData prx null!" << endl;
        return -1;
    }

    dbagent::TDBReadReq dataReq;
    dataReq.keyIndex = 0;
    dataReq.queryType = dbagent::E_SELECT;
    dataReq.tableName = "tb_club_table";

    vector<dbagent::TField> fields;
    dbagent::TField tfield;

    tfield.colArithType = E_NONE;
    tfield.colName = "club_id";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "room_key";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "room_name";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "create_name";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "create_avatar";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "dalao";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "room_type";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "game_type";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "status";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "small_blind";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "big_blind";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "pre_blind";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "max_time";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "cost_time";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "start_time";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "cur_seat";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "max_seat";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "insure";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "entry_fee";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "service_fee";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "score";
    fields.push_back(tfield);

    dataReq.fields = fields;

    //where条件组
    vector<dbagent::ConditionGroup> conditionGroups;
    dbagent::ConditionGroup conditionGroup;
    conditionGroup.relation = dbagent::AND;
    vector<dbagent::Condition> conditions;
    dbagent::Condition condition;
    condition.condtion = dbagent::E_EQ;
    condition.colType = dbagent::STRING;
    condition.colName = "room_key";
    condition.colValues = roomKey;
    conditions.push_back(condition);

    condition.condtion = dbagent::E_EQ;
    condition.colType = dbagent::INT;
    condition.colName = "create_time";
    condition.colValues = I2S(createdTime);
    conditions.push_back(condition);

    condition.condtion = dbagent::E_LT;
    condition.colType = dbagent::INT;
    condition.colName = "status";
    condition.colValues = I2S(3);
    conditions.push_back(condition);

    conditionGroup.condition = conditions;
    conditionGroups.push_back(conditionGroup);
    dataReq.conditions = conditionGroups;

    // 指定返回的行数的最大值
    dataReq.limit = 1;
    // 指定返回的每一行的偏移量
    dataReq.limit_from = 0;

    dbagent::TDBReadRsp dataRsp;
    iRet = prx->read(dataReq, dataRsp);
    if (iRet != 0 || dataRsp.iResult != 0)
    {
        ROLLLOG_ERROR << "ManageTableGetData failed!" << " iRet:" << iRet << ", iResult:" << dataRsp.iResult <<  ", dataReq:" << printTars(dataReq) << endl;
        return -2;
    }

    for (auto it = dataRsp.records.begin(); it != dataRsp.records.end(); ++it)
    {
        for (auto itField = it->begin(); itField != it->end(); ++itField)
        {
            if (itField->colName == "club_id")
            {
                info.cId = S2L(itField->colValue);
            }
            else if (itField->colName == "room_key")
            {
                info.roomKey = itField->colValue;
            }
            else if (itField->colName == "room_name")
            {
                info.roomName = itField->colValue;
            }
            else if (itField->colName == "create_name")
            {
                info.createName = itField->colValue;
            }
            else if (itField->colName == "create_avatar")
            {
                info.createAvatar = itField->colValue;
            }
            else if (itField->colName == "dalao")
            {
                info.dalao = S2I(itField->colValue);
            }
            else if (itField->colName == "room_type")
            {
                info.roomType = S2I(itField->colValue);
            }
            else if (itField->colName == "game_type")
            {
                info.gameType = S2I(itField->colValue);
            }
            else if (itField->colName == "status")
            {
                info.status = S2I(itField->colValue);
            }
            else if (itField->colName == "small_blind")
            {
                info.smallBlind = S2I(itField->colValue);
            }
            else if (itField->colName == "big_blind")
            {
                info.bigBlind = S2I(itField->colValue);
            }
            else if (itField->colName == "pre_blind")
            {
                info.preBlind = S2I(itField->colValue);
            }
            else if (itField->colName == "max_time")
            {
                info.maxTime = S2I(itField->colValue);
            }
            else if (itField->colName == "cost_time")
            {
                info.costTime = S2I(itField->colValue);
            }
            else if (itField->colName == "start_time")
            {
                info.startTime = S2I(itField->colValue);
            }
            else if (itField->colName == "cur_seat")
            {
                info.curSeat = S2I(itField->colValue);
            }
            else if (itField->colName == "max_seat")
            {
                info.maxSeat = S2I(itField->colValue);
            }
            else if (itField->colName == "insure")
            {
                int insure = S2I(itField->colValue);
                info.insure = insure == 1 ? true : false;
            }
            else if (itField->colName == "entry_fee")
            {
                info.iEntryFee = S2I(itField->colValue);
            }
            else if (itField->colName == "service_fee")
            {
                info.iServiceFee = S2I(itField->colValue);
            }
            else if (itField->colName == "score")
            {
                info.iScore = S2I(itField->colValue);
            }
            else if (itField->colName == "rebuy_count")
            {
                info.iReBuyCount = S2I(itField->colValue);
            }
            else if (itField->colName == "delay_signup")
            {
                info.iDelaySignUp = S2I(itField->colValue);
            }
            else if (itField->colName == "bet_limit")
            {
                info.bBetLimit = S2I(itField->colValue);
            }
            else if (itField->colName == "media")
            {
                info.bMedia = S2I(itField->colValue);
            }
            else if (itField->colName == "blind")
            {
                info.bBlind = S2I(itField->colValue);
            }
        }
    }

    return iRet;

    __CATCH__
    FUNC_EXIT("", iRet);
    return -1;
}

// 更新俱乐部桌子信息
tars::Int32 ManageTableModify(const DBAgentServantPrx prx, tars::Int64 cId, std::string roomKey, int createTime, int flag, int startTime, int costTime, int status, int seatNum)
{
    FUNC_ENTRY("");
    int iRet = 0;
    __TRY__

    if (!prx)
    {
        ROLLLOG_ERROR << "ManageTableModify prx null!" << endl;
        return -1;
    }

    dataproxy::TWriteDataReq dataReq;
    dataReq.resetDefautlt();
    dataReq.keyName = I2S(E_REDIS_TYPE_HASH) + ":" + I2S(CLUB_TABLE_INFO) + ":" + L2S(cId);
    dataReq.operateType = E_REDIS_WRITE;
    dataReq.paraExt.resetDefautlt();
    dataReq.paraExt.queryType = dbagent::E_UPDATE;
    dataReq.clusterInfo.resetDefautlt();
    dataReq.clusterInfo.busiType = E_REDIS_PROPERTY;
    dataReq.clusterInfo.frageFactorType = E_FRAGE_FACTOR_USER_ID;
    dataReq.clusterInfo.frageFactor = cId;

    vector<TField> fields;
    TField tfield;
    tfield.colArithType = E_NONE;
    if (flag == 2)
    {
        tfield.colName = "cur_seat";
        tfield.colType = INT;
        tfield.colValue = I2S(seatNum);
        fields.push_back(tfield);
    }
    else if (flag == 1)
    {
        tfield.colName = "status";
        tfield.colType = INT;
        tfield.colValue = I2S(status);
        fields.push_back(tfield);
    }
    else
    {
        tfield.colName = "status";
        tfield.colType = INT;
        tfield.colValue = I2S(status);
        fields.push_back(tfield);

        tfield.colName = "start_time";
        tfield.colType = INT;
        tfield.colValue = I2S(startTime);
        tfield.colArithType = E_NONE;
        fields.push_back(tfield);

        tfield.colName = "cost_time";
        tfield.colType = INT;
        tfield.colValue = I2S(costTime);
        tfield.colArithType = E_NONE;
        fields.push_back(tfield);
    }

    dataReq.fields = fields;

    auto &conditionFields = dataReq.paraExt.fields;
    tfield.resetDefautlt();
    tfield.colValue = roomKey;
    conditionFields["room_key"] = tfield;
    tfield.colValue = I2S(createTime);
    conditionFields["create_time"] = tfield;

    dataproxy::TWriteDataRsp dataRsp;
    iRet = prx->redisWrite(dataReq, dataRsp);
    if (iRet != 0 || dataRsp.iResult != 0)
    {
        ROLLLOG_ERROR << "ManageTableModify failed! iRet:" << iRet << ", iResult:" << dataRsp.iResult << ", dataReq:" << dataReq << endl;
        return -2;
    }

    ROLLLOG_DEBUG << "ManageTableModify ok! dataReq:" << printTars(dataReq) << ", dataRsp:" << printTars(dataRsp) << endl;

    __CATCH__
    FUNC_EXIT("", iRet);
    return iRet;
}

// 更新俱乐部桌子详情信息
tars::Int32 ManageTableModifyDetails(const DBAgentServantPrx prx, tars::Int64 cId, std::string roomKey, int startTime)
{
    FUNC_ENTRY("");
    int iRet = 0;
    __TRY__

    if (!prx)
    {
        ROLLLOG_ERROR << "ManageTableModifyDetails prx null!" << endl;
        return -1;
    }

    dataproxy::TWriteDataReq dataReq;
    dataReq.resetDefautlt();
    dataReq.keyName = I2S(E_REDIS_TYPE_HASH) + ":" + I2S(CLUB_TABLE_DETAILS) + ":" + L2S(cId);
    dataReq.operateType = E_REDIS_WRITE;
    dataReq.paraExt.resetDefautlt();
    dataReq.paraExt.queryType = dbagent::E_UPDATE;
    dataReq.clusterInfo.resetDefautlt();
    dataReq.clusterInfo.busiType = E_REDIS_PROPERTY;
    dataReq.clusterInfo.frageFactorType = E_FRAGE_FACTOR_USER_ID;
    dataReq.clusterInfo.frageFactor = cId;

    vector<TField> fields;
    TField tfield;
    tfield.colArithType = E_NONE;
    
    tfield.colName = "status";
    tfield.colType = INT;
    tfield.colValue = I2S(3);
    fields.push_back(tfield);

    dataReq.fields = fields;

    auto &conditionFields = dataReq.paraExt.fields;
    tfield.resetDefautlt();
    tfield.colValue = roomKey;
    conditionFields["room_key"] = tfield;
    tfield.colValue = I2S(startTime);
    conditionFields["start_time"] = tfield;

    dataproxy::TWriteDataRsp dataRsp;
    iRet = prx->redisWrite(dataReq, dataRsp);
    if (iRet != 0 || dataRsp.iResult != 0)
    {
        ROLLLOG_ERROR << "ManageTableModifyDetails failed! iRet:" << iRet << ", iResult:" << dataRsp.iResult << ", dataReq:" << printTars(dataReq) << endl;
        return -2;
    }

    ROLLLOG_DEBUG << "ManageTableModifyDetails ok! dataReq:" << printTars(dataReq) << ", dataRsp:" << printTars(dataRsp) << endl;

    __CATCH__
    FUNC_EXIT("", iRet);
    return iRet;
}

// 获取创建且未结束的桌子数量
tars::Int32 ManageTableGetCreatedCount(const DBAgentServantPrx prx, tars::Int64 uId, int &count)
{
    FUNC_ENTRY("");
    int iRet = 0;
    __TRY__

    if (!prx)
    {
        ROLLLOG_ERROR << "ManageTableGetCreatedCount prx null!" << endl;
        return -1;
    }

    dbagent::TDBReadReq dataReq;
    dataReq.keyIndex = 0;
    dataReq.queryType = dbagent::E_SELECT;
    dataReq.tableName = "tb_club_table";

    vector<dbagent::TField> fields;
    dbagent::TField tfield;
    tfield.colArithType = E_NONE;
    tfield.colName = "id";
    fields.push_back(tfield);

    dataReq.fields = fields;

    //where条件组
    vector<dbagent::ConditionGroup> conditionGroups;
    dbagent::ConditionGroup conditionGroup;
    conditionGroup.relation = dbagent::AND;
    vector<dbagent::Condition> conditions;
    dbagent::Condition condition;
    condition.condtion = dbagent::E_EQ;
    condition.colType = dbagent::BIGINT;
    condition.colName = "created_id";
    condition.colValues = L2S(uId);
    conditions.push_back(condition);

    condition.condtion = dbagent::E_LT;
    condition.colType = dbagent::INT;
    condition.colName = "status";
    condition.colValues = I2S(3);
    conditions.push_back(condition);
    conditionGroup.condition = conditions;
    conditionGroups.push_back(conditionGroup);
    dataReq.conditions = conditionGroups;

    dbagent::TDBReadRsp dataRsp;
    iRet = prx->read(dataReq, dataRsp);
    if (iRet != 0 || dataRsp.iResult != 0)
    {
        ROLLLOG_ERROR << "ManageTableGetCreatedCount failed!" << ", iRet:" << iRet << ", iResult:" << dataRsp.iResult << ", dataReq:" << printTars(dataReq) << endl;
        return -2;
    }

    if (dataRsp.totalcount > 0)
    {
        count = dataRsp.totalcount;
    }

    __CATCH__

    FUNC_EXIT("", iRet);
    return iRet;
}

// 修正俱乐部桌子状态(游戏启动后)
tars::Int32 ManageTableRestore(const DBAgentServantPrx prx, const string sGameType)
{
    FUNC_ENTRY("");
    int iRet = 0;
    __TRY__

    if (!prx)
    {
        ROLLLOG_ERROR << "ManageTableRestore prx null!" << endl;
        return -1;
    }

    dbagent::TDBWriteReq dataReq;
    dataReq.keyIndex = 0;
    dataReq.queryType = dbagent::E_UPDATE;
    dataReq.tableName = "tb_club_table";

    vector<dbagent::TField> fields;
    dbagent::TField tfield;
    tfield.colArithType = E_NONE;
    tfield.colName = "status";
    tfield.colType = dbagent::INT;
    tfield.colValue = I2S(4);
    fields.push_back(tfield);

    dataReq.fields = fields;

    //where条件组
    vector<dbagent::ConditionGroup> conditionGroups;
    dbagent::ConditionGroup conditionGroup;
    conditionGroup.relation = dbagent::AND;
    vector<dbagent::Condition> conditions;
    dbagent::Condition condition;
    condition.condtion = dbagent::E_LT;
    condition.colType = dbagent::BIGINT;
    condition.colName = "status";
    condition.colValues = L2S(3);
    conditions.push_back(condition);

    condition.condtion = dbagent::E_IN;
    condition.colType = dbagent::INT;
    condition.colName = "game_type";
    condition.colValues = sGameType;
    conditions.push_back(condition);

    conditionGroup.condition = conditions;
    conditionGroups.push_back(conditionGroup);
    dataReq.conditions = conditionGroups;

    dbagent::TDBWriteRsp dataRsp;
    iRet = prx->write(dataReq, dataRsp);
    if (iRet != 0 || dataRsp.iResult != 0)
    {
        ROLLLOG_ERROR << "ManageTableRestore failed! dataReq:" << printTars(dataReq) << ", dataRsp:" << printTars(dataRsp) << endl;
        return -2;
    }

    __CATCH__
    FUNC_EXIT("", iRet);
    return iRet;
}

//TODO 更新俱乐部桌子信息(牛仔)
tars::Int32 ManageTableModifyCowBoy(const DBAgentServantPrx prx, tars::Int64 clubId, std::string roomKey, int createTime, int costTime, int playerCount, long buyIn, int buyInCount, long bets, long pays, long fees)
{
    FUNC_ENTRY("");
    int iRet = 0;
    __TRY__

    if (!prx)
    {
        ROLLLOG_ERROR << "ManageTableModifyCowBoy prx null!" << endl;
        return -1;
    }

    dataproxy::TWriteDataReq dataReq;
    dataReq.resetDefautlt();
    dataReq.keyName = I2S(E_REDIS_TYPE_HASH) + ":" + I2S(CLUB_TABLE_INFO) + ":" + L2S(clubId);
    dataReq.operateType = E_REDIS_WRITE;
    dataReq.paraExt.resetDefautlt();
    dataReq.paraExt.queryType = dbagent::E_UPDATE;
    dataReq.clusterInfo.resetDefautlt();
    dataReq.clusterInfo.busiType = E_REDIS_PROPERTY;
    dataReq.clusterInfo.frageFactorType = E_FRAGE_FACTOR_USER_ID;
    dataReq.clusterInfo.frageFactor = clubId;

    vector<TField> fields;
    TField tfield;
    tfield.colArithType = E_NONE;
  
    tfield.colName = "cost_time";
    tfield.colType = INT;
    tfield.colValue = I2S(costTime);
    fields.push_back(tfield);
    
    tfield.colName = "max_seat";
    tfield.colType = INT;
    tfield.colValue = I2S(playerCount);
    fields.push_back(tfield);

    tfield.colName = "min_gold";
    tfield.colType = BIGINT;
    tfield.colValue = L2S(buyIn);
    fields.push_back(tfield);

    tfield.colName = "max_gold";
    tfield.colType = INT;
    tfield.colValue = I2S(buyInCount);
    fields.push_back(tfield);

    tfield.colName = "entry_fee";
    tfield.colType = BIGINT;
    tfield.colValue = L2S(bets);
    fields.push_back(tfield);

    tfield.colName = "score";
    tfield.colType = BIGINT;
    tfield.colValue = L2S(pays);
    fields.push_back(tfield);

    tfield.colName = "service_fee";
    tfield.colType = BIGINT;
    tfield.colValue = L2S(fees);
    fields.push_back(tfield);

    dataReq.fields = fields;

    auto &conditionFields = dataReq.paraExt.fields;
    tfield.resetDefautlt();
    tfield.colValue = roomKey;
    conditionFields["room_key"] = tfield;
    tfield.colValue = I2S(createTime);
    conditionFields["create_time"] = tfield;

    dataproxy::TWriteDataRsp dataRsp;
    iRet = prx->redisWrite(dataReq, dataRsp);
    if (iRet != 0 || dataRsp.iResult != 0)
    {
        ROLLLOG_ERROR << "modify club table err, iRet: " << iRet << ", iResult: " << dataRsp.iResult << endl;
        return -2;
    }

    ROLLLOG_DEBUG << "modify club table, iRet: " << iRet << ", dataRsp: " << printTars(dataRsp) << endl;

    __CATCH__
    FUNC_EXIT("", iRet);
    return iRet;
}

tars::Int32 ManageTableCountForUnion(const DBAgentServantPrx prx, long nId, long cId, int &count)
{
    FUNC_ENTRY("");
    int iRet = 0;
    __TRY__

    if (!prx)
    {
        ROLLLOG_ERROR << "ManageTableCountForUnion prx null!" << endl;
        return -1;
    }

    dbagent::TDBReadReq dataReq;
    dataReq.keyIndex = 0;
    dataReq.queryType = dbagent::E_SELECT;
    dataReq.tableName = "tb_club_table";

    vector<dbagent::TField> fields;
    dbagent::TField tfield;
    tfield.colArithType = E_NONE;
    tfield.colName = "club_id";
    fields.push_back(tfield);

    dataReq.fields = fields;

    //where条件组
    vector<dbagent::ConditionGroup> conditionGroups;
    dbagent::ConditionGroup conditionGroup;
    conditionGroup.relation = dbagent::AND;
    vector<dbagent::Condition> conditions;
    dbagent::Condition condition;
    
    condition.condtion = dbagent::E_EQ;
    condition.colType = dbagent::BIGINT;
    condition.colName = "union_id";
    condition.colValues = L2S(nId);
    conditions.push_back(condition);

    condition.condtion = dbagent::E_EQ;
    condition.colType = dbagent::INT;
    condition.colName = "sync_union";
    condition.colValues = I2S(1);
    conditions.push_back(condition);

    condition.condtion = dbagent::E_LT;
    condition.colType = dbagent::INT;
    condition.colName = "status";
    condition.colValues = I2S(3);
    conditions.push_back(condition); 

    conditionGroup.condition = conditions;
    conditionGroups.push_back(conditionGroup);

    conditions.clear();
    conditionGroup.relation = dbagent::AND;
    condition.condtion = dbagent::E_EQ;
    condition.colType = dbagent::BIGINT;
    condition.colName = "club_id";
    condition.colValues = L2S(cId);
    conditions.push_back(condition);

    condition.condtion = dbagent::E_LT;
    condition.colType = dbagent::INT;
    condition.colName = "status";
    condition.colValues = I2S(3);
    conditions.push_back(condition);

    conditionGroup.condition = conditions;
    conditionGroups.push_back(conditionGroup);

    dataReq.conditions = conditionGroups;

    dataReq.groupRelation = dbagent::OR;


    dbagent::TDBReadRsp dataRsp;
    iRet = prx->read(dataReq, dataRsp);
    if (iRet != 0 || dataRsp.iResult != 0)
    {
        ROLLLOG_ERROR << "ManageTableCountForUnion failed!" << " iRet:" << iRet << ", iResult:" << dataRsp.iResult << ", dataReq:" << printTars(dataReq) << endl;
        return -2;
    }

    if (dataRsp.totalcount > 0)
    {
        count = dataRsp.totalcount;
    }

    __CATCH__
    
    FUNC_EXIT("", iRet);
    return iRet;
}
